Series Expansion

In this lesson, we will learn about Taylor series and formal power series.

Taylor series#

SymPy expressions can be expanded as Taylor series using the series() function from the SymPy module.

Taylor series about x=ax=a is given by:

f(x)=∑n=0∞f(n)an!(x−a)nf(x)=\sum_{n=0}^{\infty}\frac{f^{(n)}a}{n!}(x-a)^n

where f(n)f^{(n)} is the nthn^{th} derivative of ff.

For a generic expansion, we only need to specify the expression and the variable in the expansion for which the expression is to be expanded.

series(f(x), x)

or

f(x).series(x)

Let’s see a basic implementation of the series() function below:

For numerical evaluations and plotting, we need to remove the trailing O(n) term. This can be done using the removeO() method:

Additionally, we can also specify the point a around which we want to expand and the maximum term power, maxTerm:

series(f(x), x, a, maxTerm)

By default, the series() function expands the expression around 00, i.e a=0.

Use the help(Basic.series) command for more information on series().

Substituting values#

Using subs(), we can substitute the value of variables, which is useful when plotting data.

subs() takes a dictionary as its input argument, where variables are the keys.

subs({x: 1, y: 4})

Formal power series#

A more useful series that can be computed using SymPy is the formal power series using the fps() function.

fps(f(x), x, a)

x is the variable for which the series is computed and a is the variable about which the series is computed.

The advantage this has over the power series is that it returns the explicit formula for the coefficients of the series rather than just computing the first few terms:

∑k=0∞akxk\sum_{k=0}^{\infty}a_kx^k

The fps() function returns the formal series expansion of the function in the form of a FormalPowerSeries object. fps() is used in combination with its truncate(n) method which returns the first n terms of the series.

fps(f(x), x, a).truncate(n)

removeO() is used to remove the trailing O(n) term.

subs() is used to substitute the value of a variable.

Let’s compute the formal power series of exe^x:

The real ‘power’ of the formal power series#

This may seem similar to the computation of the power series but let’s see how the formal power series is more useful. Since we have the generic formula of the coefficients, we can extract out a single term as well. Suppose we want to extract the 15th15^{th} term from the expansion of exe^x:

In line 6, we used the index to extract the specific term that we needed.

The value of the index corresponds to the power of the variable for which we computed the series, that is, to get the coefficient of the 15th15^{th} value, we used the index 15.


In the next lesson, we will learn about solving equations in SymPy.

Quiz 7!

Solving Equations